/*
* Entourage
* Copyright 2011 INF532@Galatasaray University & Vincent Labatut 
* 
* This file is part of Entourage.
* 
* Entourage is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Entourage is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with Entourage. If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.appspot.entourage.freebasechecker;

import java.util.regex.Pattern;

import com.appspot.entourage.common.Person;

/**
 * This class is used to to convert non-unicode URL keys to Freebase Supported
 * Unicode keys.
 * @author Berk PELENK <bpelenk@beengames.com>
 * @version 1
 * @see Person
 *
 */
public class UnicodeConverter {
	/** this field represents chars which can be positioned at the beginning of the word to be queryable by Freebase database */
	static String mqlkey_start = "A-Za-z0-9";
	
	/** this field represents valid chars for Freebase as String */
	static String mqlkey_char = "A-Za-z0-9_-";

	/** this field represents valid chars for Freebase as RegExp */
	static Pattern MQLKEY_VALID = Pattern.compile("^[' + mqlkey_start + '][' + mqlkey_char + ']*$");

	/** this field represents chars which require escaping */
	static Pattern MQLKEY_CHAR_MUSTQUOTE = Pattern.compile("([^" + mqlkey_char + "])", 'g');

	/**
	 * This method converts the specified character into freebase 
	 * supported unicode string
	 * @param b character to convert to unicode
	 * @return 4 chars length unicode representation starting with $
	 * @author Berk PELENK <bpelenk@beengames.com>
	 */
	public static String convert(String b) {
        int hexval =  b.codePointAt(0);
		String hex = Integer.toString(hexval,16).toUpperCase();
        if (hex.length() == 2)
            hex = "00" + hex;
        return '$' + hex;
    };
    
	/**
	 * This method converts a string to turn it into a valid 
	 * mql /type/key/value
	 * @param b character to convert to unicode
	 * @return searched text replaced with freebase supported unicode 
	 * representation
	 * @author Berk PELENK <bpelenk@beengames.com>
	 */
	public static String mqlkey_quote(String s) {
	    if (MQLKEY_VALID.matcher(s).matches())
	        return s;
	    String x = "";
	    char[] sarr = s.toCharArray();
	    for(int i=0; i<sarr.length; i++)
	    {
	    	if(MQLKEY_CHAR_MUSTQUOTE.matcher(sarr[i]+"").matches())
	    		x+= convert(sarr[i]+"");
	    	else
	    		x+= sarr[i]+"";
	    }

	    if (x.charAt(0) == '-' || x.charAt(0) == '_') {
	        x = convert(x.charAt(0)+"") + x.substring(1);
	    }
	    
	    return x;
	}
}
